package com.ttdev.mmms.controller.login;

import com.ttdev.mmms.config.SecurityUtils;
import com.ttdev.mmms.entity.StoreInfo;
import com.ttdev.mmms.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * 店长登陆
 *周义勇
 */
@Controller
@RequestMapping("/store")
public class StoreLoginController {

    @Autowired
    private StoreService storeService;

    @RequestMapping("/login")
    public String login(String username,String password, HttpSession session, RedirectAttributes attr){
        String pwd = SecurityUtils.md5Hex(password);//MD5技术加密
        StoreInfo storeInfo = storeService.findPwd(username);
        if (storeInfo!=null){
            if(storeInfo.getPassword().equals(pwd)){
                session.setAttribute("storeManage",storeInfo);
                //以秒为单位，即在没有活动90分钟后，session将失效
                session.setMaxInactiveInterval(90*60);
                return "redirect:/storeIndex";
            }else {
                attr.addFlashAttribute("error", "密码错误!");
                return "redirect:/";
            }
        }else{
            attr.addFlashAttribute("error", "账号不存在!");
            return "redirect:/";
        }
    }
}